import Vue from 'vue'
import VueRouter from 'vue-router'
const Home =()=>import('@/views/Home.vue')
import Login from "../views/login"
import Shouye from "../views/shouye"
import Welcome from "../views/welcome"
import Users from "../views/user/Users"
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

  const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/shouye',
    name: 'Shouye',
    component: Shouye,
    children:[
      {
        path: 'welcome',
        name: 'Welcome',
        component: Welcome
      },
      {
        path:"/users",
        name: 'Users',
        component: Users
      },
      {
        path:"",
        redirect:"/shouye/welcome"
      }
    ]
  },
  {
    path:'/',
    redirect:'/login'
  },
  
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path=="/login")
    return next()
    //获取token
   let tokenStr=window.sessionStorage.getItem("token")
  if(!tokenStr) return next("/login")
  next()
})

export default router
